package com.ulook.planetmanager.ui.home.user;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.ulook.planetmanager.base.BaseViewModel2;
import com.ulook.planetmanager.common.AppServer;
import com.ulook.planetmanager.model.TUser;
import com.ulook.planetmanager.util.ThrowException;
import com.ulook.planetmanager.util.net.AsynRequestUtil;
import com.ulook.planetmanager.util.net.OkHttpException;
import com.ulook.planetmanager.util.net.RequestParams;
import com.ulook.planetmanager.util.net.ResponseCallback;


/**
 * 描述：
 *
 * @author L1998
 * @data 2021/5/1
 */
public class UserDetailViewModel extends BaseViewModel2 {

    private TUser user;
    private int currUser;
    private MutableLiveData<TUser> userInfo = new MutableLiveData<>();

    public LiveData<TUser> getUserInfo() {
        return userInfo;
    }

    public TUser getUser() {
        return user;
    }

    public void setUser(TUser user) {
        this.user = user;
        userInfo.setValue(user);
    }

    public int getCurrUser() {
        return currUser;
    }

    public void setCurrUser(int currUser) {
        this.currUser = currUser;
    }


    /**
     * 点击冻结
     */
    public void freeze(){
        RequestParams params = new RequestParams();
        params.put("id", user.getId());
        if(userInfo.getValue().getState() == 0){
            params.put("state", 1);
        }else {
            params.put("state", 0);
        }
        AsynRequestUtil.post(AppServer.ROOT_PATH + "/manage/freeze", params, null, null);
    }

    /**
     * 删除账户
     */
    public void delete(){
        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                setToastText("删除成功");
                userInfo.setValue(null);
            }
            @Override
            public void onFailure(OkHttpException failuer) {
                setToastText("删除失败");
            }
        };
        RequestParams params = new RequestParams();
        params.put("id", user.getId());

        AsynRequestUtil.post(AppServer.ROOT_PATH + "/manage/deleteUser", params, null, callback);
    }

    /**
     * 获取用户信息
     */
    public boolean initUserInfo(int id) {

        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                user = (TUser) res;
                userInfo.setValue((TUser) res);
            }

            @Override
            public void onFailure(OkHttpException failuer) {
                setToastText("获取用户信息失败");
                ThrowException.print(failuer, "获取用户信息失败");
            }
        };
        RequestParams params = new RequestParams();
        params.put("id", id);
        AsynRequestUtil.post(AppServer.ROOT_PATH + "/home/getUserInfo", params, TUser.class, callback);

        return true;

    }
}
